package org.zhadoop.streaming.util.datatype;

import java.math.BigDecimal;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zhadoop.streaming.exception.StreamingException;

import com.google.common.base.Strings;

/**
 * 
 * @author zwd
 *
 */
public class DecimalParser implements DataTypeParser {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5309180671231177861L;

	private static final Logger LOG = LoggerFactory.getLogger(DecimalParser.class);
	
	
	@Override
	public Object createValue(String value) throws StreamingException {
		if(Strings.isNullOrEmpty(value)){
			return null;
		}
		try{
			return new BigDecimal(value);
		}catch(NumberFormatException e){
			LOG.warn("Failed to create {} instance." , BigDecimal.class.getName());
			throw new StreamingException("Failed to create " + BigDecimal.class.getName() + " instance.");
		}
	}

	@Override
	public String toStringValue(Object value) throws StreamingException {
		if(null == value){
			return null;
		}
		return value.toString();
	}

}
